<template>
  <di style="padding: 20px;width: 80%">
    <h2 style="margin-left: 20px;margin-top: 0px">新增用户</h2>
    <!--  数据表单  -->
    <el-form :rules="rules" ref="ruleForm" :inline="true" :model="form" style="width: 90%" label-width="100px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model.number="form.age" placeholder="请输入年龄"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="form.sex" placeholder="请选择性别">
          <el-option label="男" value="男"></el-option>
          <el-option label="女" value="女"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入联系电话"></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="form.address" placeholder="请输入地址"></el-input>
      </el-form-item>
    </el-form>

    <!-- 按键 -->
    <div style="text-align: center; margin-top: 30px">
      <el-button type="primary" @click="save('ruleForm')" size="mediu">提交</el-button>
<!--      <el-button type="danger">取消</el-button>-->
    </div>
  </di>
</template>

<script>
import request from "@/utils/request";
export default {
  name: "AddUser.vue",
  data() {
    //检查名字是否正确
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('名字不能为空'));
      }else {
        callback();
      }
    };
    //检查年龄输入是否正确
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('年龄不能为空'));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error('请输入数字值'));
        } else {
          if (value < 0 || value > 120) {
            callback(new Error('必须为0~120岁'));
          } else {
            callback();
          }
        }
      });
    };
    //检查时候选择性别
    var checkSex = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请选择性别'));
      }else {
        callback();
      }
    };
    //检查时候选择联系电话
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入联系电话'));
      }else {
        callback();
      }
    };
    //检查时候选择性别
    var checkAddress = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入地址'));
      }else {
        callback();
      }
    };
    return {
      form: { account:'0' },
      ruleForm: {
        name: '',
        age: '',
        sex: '',
        phone: '',
        address: ''
      },
      rules: {
        name: [
          { validator:checkName , trigger: 'blur' },
        ],
        age: [
          { validator:checkAge , trigger: 'blur' },
        ],
        sex: [
          { validator:checkSex , trigger: 'change' }
        ],
        phone: [
          { validator:checkPhone , trigger: 'blur' }
        ],
        address: [
          { validator:checkAddress , trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    save(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          request.post('/user/save',this.form).then(res => {
            if (res.code === '200') {
              this.$notify.success('新增成功')
              this.$refs['ruleForm'].resetFields()    //添加后清空表单
            }else {
              this.$notify.error(res.msg)
            }
          })
        }else {
          console.log('error submit!!');
          return false;
        }
      })
    }
  }
}
</script>
